
extern "C" {
    fn sendmsg(__fd: i32, __message: *const msghdr, __flags: i32) -> ssize_t;
    fn writev(__fd: i32, __iovec: *const iovec, __count: i32) -> ssize_t;
    fn ntohl(__netlong: uint32_t) -> uint32_t;
    fn ntohs(__netshort: uint16_t) -> uint16_t;
    fn htonl(__hostlong: uint32_t) -> uint32_t;
    fn htons(__hostshort: uint16_t) -> uint16_t;
    fn __errno_location() -> *mut i32;
    fn calloc(_: u64, _: u64) -> *mut ();
    fn free(_: *mut ());
    fn abort() -> !;
    fn memcpy(_: *mut (), _: *const (), _: u64) -> *mut ();
    fn memset(_: *mut (), _: i32, _: u64) -> *mut ();
    fn close(__fd: i32) -> i32;
    fn recallocarray(_: *mut (), _: size_t, _: size_t, _: size_t) -> *mut ();
    fn htonll(_: uint64_t) -> uint64_t;
    fn ntohll(_: uint64_t) -> uint64_t;
    fn freezero(_: *mut (), _: size_t);
}
pub type __uint8_t = u8;
pub type __uint16_t = u16;
pub type __uint32_t = u32;
pub type __uint64_t = u64;
pub type __ssize_t = i64;
pub type __caddr_t = *mut i8;
pub type __socklen_t = u32;
pub type ssize_t = __ssize_t;
pub type caddr_t = __caddr_t;
pub type size_t = u64;
#[derive(Copy, Clone)]
#[repr(C)]
pub struct iovec {
    pub iov_base: *mut (),
    pub iov_len: size_t,
}
pub type socklen_t = __socklen_t;
#[derive(Copy, Clone)]
#[repr(C)]
pub struct msghdr {
    pub msg_name: *mut (),
    pub msg_namelen: socklen_t,
    pub msg_iov: *mut iovec,
    pub msg_iovlen: size_t,
    pub msg_control: *mut (),
    pub msg_controllen: size_t,
    pub msg_flags: i32,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct cmsghdr {
    pub cmsg_len: size_t,
    pub cmsg_level: i32,
    pub cmsg_type: i32,
    pub __cmsg_data: [u8; 0],
}
pub type C2RustUnnamed = u32;
pub const SCM_CREDENTIALS: C2RustUnnamed = 2;
pub const SCM_RIGHTS: C2RustUnnamed = 1;
pub type uint32_t = __uint32_t;
pub type uint16_t = __uint16_t;
pub type uint8_t = __uint8_t;
pub type uint64_t = __uint64_t;
#[derive(Copy, Clone)]
#[repr(C)]
pub struct ibuf {
    pub entry: C2RustUnnamed_0,
    pub buf: *mut u8,
    pub size: size_t,
    pub max: size_t,
    pub wpos: size_t,
    pub rpos: size_t,
    pub fd: i32,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct C2RustUnnamed_0 {
    pub tqe_next: *mut ibuf,
    pub tqe_prev: *mut *mut ibuf,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct msgbuf {
    pub bufs: C2RustUnnamed_1,
    pub queued: uint32_t,
    pub fd: i32,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct C2RustUnnamed_1 {
    pub tqh_first: *mut ibuf,
    pub tqh_last: *mut *mut ibuf,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub union C2RustUnnamed_2 {
    pub hdr: cmsghdr,
    pub buf: [i8; 24],
}
#[no_mangle]
pub extern "C" fn ibuf_open(mut len: size_t) -> *mut ibuf {
    let mut buf: *mut ibuf = std::ptr::null_mut::<ibuf>();
    if len == 0 as u64 {
        (unsafe { *__errno_location() = 22 });
        return std::ptr::null_mut::<ibuf>();
    }
    buf = (unsafe { calloc(1 as i32 as u64, ::core::mem::size_of::<ibuf>() as u64) }) as *mut ibuf;
    if buf.is_null() {
        return std::ptr::null_mut::<ibuf>();
    }
    (unsafe { (*buf).buf = calloc(len, 1 as u64) as *mut u8 });
    if (unsafe { (*buf).buf }).is_null() {
        (unsafe { free(buf as *mut ()) });
        return std::ptr::null_mut::<ibuf>();
    }
    (unsafe { (*buf).max = len });
    (unsafe { (*buf).size = (*buf).max });
    (unsafe { (*buf).fd = -(1 as i32) });
    return buf;
}
#[no_mangle]
pub extern "C" fn ibuf_dynamic(mut len: size_t, mut max: size_t) -> *mut ibuf {
    let mut buf: *mut ibuf = std::ptr::null_mut::<ibuf>();
    if max == 0 as u64 || max < len {
        (unsafe { *__errno_location() = 22 });
        return std::ptr::null_mut::<ibuf>();
    }
    buf = (unsafe { calloc(1 as i32 as u64, ::core::mem::size_of::<ibuf>() as u64) }) as *mut ibuf;
    if buf.is_null() {
        return std::ptr::null_mut::<ibuf>();
    }
    if len > 0 as u64 {
        (unsafe { (*buf).buf = calloc(len, 1 as u64) as *mut u8 });
        if (unsafe { (*buf).buf }).is_null() {
            (unsafe { free(buf as *mut ()) });
            return std::ptr::null_mut::<ibuf>();
        }
    }
    (unsafe { (*buf).size = len });
    (unsafe { (*buf).max = max });
    (unsafe { (*buf).fd = -(1 as i32) });
    return buf;
}
extern "C" fn ibuf_realloc(mut buf: *mut ibuf, mut len: size_t) -> i32 {
    let mut b: *mut u8 = std::ptr::null_mut::<u8>();
    if len > (18446744073709551615 as u64).wrapping_sub(unsafe { (*buf).wpos })
        || (unsafe { (*buf).wpos }).wrapping_add(len) > (unsafe { (*buf).max })
    {
        (unsafe { *__errno_location() = 34 });
        return -(1 as i32);
    }
    b = (unsafe {
        recallocarray(
            (*buf).buf as *mut (),
            (*buf).size,
            ((*buf).wpos).wrapping_add(len), 1 as size_t,
        )
    }) as *mut u8;
    if b.is_null() {
        return -(1 as i32);
    }
    (unsafe { (*buf).buf = b });
    (unsafe { (*buf).size = ((*buf).wpos).wrapping_add(len) });
    return 0;
}
#[no_mangle]
pub extern "C" fn ibuf_reserve(mut buf: *mut ibuf, mut len: size_t) -> *mut () {
    let mut b: *mut () = std::ptr::null_mut::<()>();
    if len > (18446744073709551615 as u64).wrapping_sub(unsafe { (*buf).wpos })
        || (unsafe { (*buf).max }) == 0 as u64
    {
        (unsafe { *__errno_location() = 34 });
        return std::ptr::null_mut::<()>();
    }
    if (unsafe { (*buf).wpos }).wrapping_add(len) > (unsafe { (*buf).size }) {
        if ibuf_realloc(buf, len) == -(1 as i32) {
            return std::ptr::null_mut::<()>();
        }
    }
    b = (unsafe { ((*buf).buf).offset((*buf).wpos as isize) }) as *mut ();
    (unsafe { (*buf).wpos = ((*buf).wpos as u64).wrapping_add(len) as size_t as size_t });
    return b;
}
#[no_mangle]
pub extern "C" fn ibuf_add(
    mut buf: *mut ibuf,
    mut data: *const (),
    mut len: size_t,
) -> i32 {
    let mut b: *mut () = std::ptr::null_mut::<()>();
    b = ibuf_reserve(buf, len);
    if b.is_null() {
        return -(1 as i32);
    }
    (unsafe { memcpy(b, data, len) });
    return 0;
}
#[no_mangle]
pub extern "C" fn ibuf_add_ibuf(mut buf: *mut ibuf, mut from: *const ibuf) -> i32 {
    return ibuf_add(buf, ibuf_data(from), ibuf_size(from));
}
#[no_mangle]
pub extern "C" fn ibuf_add_buf(mut buf: *mut ibuf, mut from: *const ibuf) -> i32 {
    return ibuf_add_ibuf(buf, from);
}
#[no_mangle]
pub extern "C" fn ibuf_add_n8(mut buf: *mut ibuf, mut value: uint64_t) -> i32 {
    let mut v: uint8_t = 0;
    if value > 255 as u64 {
        (unsafe { *__errno_location() = 22 });
        return -(1 as i32);
    }
    v = value as uint8_t;
    return ibuf_add(
        buf,
        &mut v as *mut uint8_t as *const (),
        ::core::mem::size_of::<uint8_t>() as u64,
    );
}
#[no_mangle]
pub extern "C" fn ibuf_add_n16(mut buf: *mut ibuf, mut value: uint64_t) -> i32 {
    let mut v: uint16_t = 0;
    if value > 65535 as u64 {
        (unsafe { *__errno_location() = 22 });
        return -(1 as i32);
    }
    v = unsafe { htons(value as uint16_t) };
    return ibuf_add(
        buf,
        &mut v as *mut uint16_t as *const (),
        ::core::mem::size_of::<uint16_t>() as u64,
    );
}
#[no_mangle]
pub extern "C" fn ibuf_add_n32(mut buf: *mut ibuf, mut value: uint64_t) -> i32 {
    let mut v: uint32_t = 0;
    if value > 4294967295 as u64 {
        (unsafe { *__errno_location() = 22 });
        return -(1 as i32);
    }
    v = unsafe { htonl(value as uint32_t) };
    return ibuf_add(
        buf,
        &mut v as *mut uint32_t as *const (),
        ::core::mem::size_of::<uint32_t>() as u64,
    );
}
#[no_mangle]
pub extern "C" fn ibuf_add_n64(mut buf: *mut ibuf, mut value: uint64_t) -> i32 {
    value = unsafe { htonll(value) };
    return ibuf_add(
        buf,
        &mut value as *mut uint64_t as *const (),
        ::core::mem::size_of::<uint64_t>() as u64,
    );
}
#[no_mangle]
pub extern "C" fn ibuf_add_h16(mut buf: *mut ibuf, mut value: uint64_t) -> i32 {
    let mut v: uint16_t = 0;
    if value > 65535 as u64 {
        (unsafe { *__errno_location() = 22 });
        return -(1 as i32);
    }
    v = value as uint16_t;
    return ibuf_add(
        buf,
        &mut v as *mut uint16_t as *const (),
        ::core::mem::size_of::<uint16_t>() as u64,
    );
}
#[no_mangle]
pub extern "C" fn ibuf_add_h32(mut buf: *mut ibuf, mut value: uint64_t) -> i32 {
    let mut v: uint32_t = 0;
    if value > 4294967295 as u64 {
        (unsafe { *__errno_location() = 22 });
        return -(1 as i32);
    }
    v = value as uint32_t;
    return ibuf_add(
        buf,
        &mut v as *mut uint32_t as *const (),
        ::core::mem::size_of::<uint32_t>() as u64,
    );
}
#[no_mangle]
pub extern "C" fn ibuf_add_h64(mut buf: *mut ibuf, mut value: uint64_t) -> i32 {
    return ibuf_add(
        buf,
        &mut value as *mut uint64_t as *const (),
        ::core::mem::size_of::<uint64_t>() as u64,
    );
}
#[no_mangle]
pub extern "C" fn ibuf_add_zero(mut buf: *mut ibuf, mut len: size_t) -> i32 {
    let mut b: *mut () = std::ptr::null_mut::<()>();
    b = ibuf_reserve(buf, len);
    if b.is_null() {
        return -(1 as i32);
    }
    (unsafe { memset(b, 0, len) });
    return 0;
}
#[no_mangle]
pub extern "C" fn ibuf_seek(
    mut buf: *mut ibuf,
    mut pos: size_t,
    mut len: size_t,
) -> *mut () {
    if ibuf_size(buf) < pos
        || (18446744073709551615 as u64).wrapping_sub(pos) < len
        || ibuf_size(buf) < pos.wrapping_add(len)
    {
        (unsafe { *__errno_location() = 34 });
        return std::ptr::null_mut::<()>();
    }
    return (unsafe {
        ((*buf).buf)
            .offset((*buf).rpos as isize)
            .offset(pos as isize)
    }) as *mut ();
}
#[no_mangle]
pub extern "C" fn ibuf_set(
    mut buf: *mut ibuf,
    mut pos: size_t,
    mut data: *const (),
    mut len: size_t,
) -> i32 {
    let mut b: *mut () = std::ptr::null_mut::<()>();
    b = ibuf_seek(buf, pos, len);
    if b.is_null() {
        return -(1 as i32);
    }
    (unsafe { memcpy(b, data, len) });
    return 0;
}
#[no_mangle]
pub extern "C" fn ibuf_set_n8(mut buf: *mut ibuf, mut pos: size_t, mut value: uint64_t) -> i32 {
    let mut v: uint8_t = 0;
    if value > 255 as u64 {
        (unsafe { *__errno_location() = 22 });
        return -(1 as i32);
    }
    v = value as uint8_t;
    return ibuf_set(
        buf,
        pos,
        &mut v as *mut uint8_t as *const (),
        ::core::mem::size_of::<uint8_t>() as u64,
    );
}
#[no_mangle]
pub extern "C" fn ibuf_set_n16(mut buf: *mut ibuf, mut pos: size_t, mut value: uint64_t) -> i32 {
    let mut v: uint16_t = 0;
    if value > 65535 as u64 {
        (unsafe { *__errno_location() = 22 });
        return -(1 as i32);
    }
    v = unsafe { htons(value as uint16_t) };
    return ibuf_set(
        buf,
        pos,
        &mut v as *mut uint16_t as *const (),
        ::core::mem::size_of::<uint16_t>() as u64,
    );
}
#[no_mangle]
pub extern "C" fn ibuf_set_n32(mut buf: *mut ibuf, mut pos: size_t, mut value: uint64_t) -> i32 {
    let mut v: uint32_t = 0;
    if value > 4294967295 as u64 {
        (unsafe { *__errno_location() = 22 });
        return -(1 as i32);
    }
    v = unsafe { htonl(value as uint32_t) };
    return ibuf_set(
        buf,
        pos,
        &mut v as *mut uint32_t as *const (),
        ::core::mem::size_of::<uint32_t>() as u64,
    );
}
#[no_mangle]
pub extern "C" fn ibuf_set_n64(mut buf: *mut ibuf, mut pos: size_t, mut value: uint64_t) -> i32 {
    value = unsafe { htonll(value) };
    return ibuf_set(
        buf,
        pos,
        &mut value as *mut uint64_t as *const (),
        ::core::mem::size_of::<uint64_t>() as u64,
    );
}
#[no_mangle]
pub extern "C" fn ibuf_set_h16(mut buf: *mut ibuf, mut pos: size_t, mut value: uint64_t) -> i32 {
    let mut v: uint16_t = 0;
    if value > 65535 as u64 {
        (unsafe { *__errno_location() = 22 });
        return -(1 as i32);
    }
    v = value as uint16_t;
    return ibuf_set(
        buf,
        pos,
        &mut v as *mut uint16_t as *const (),
        ::core::mem::size_of::<uint16_t>() as u64,
    );
}
#[no_mangle]
pub extern "C" fn ibuf_set_h32(mut buf: *mut ibuf, mut pos: size_t, mut value: uint64_t) -> i32 {
    let mut v: uint32_t = 0;
    if value > 4294967295 as u64 {
        (unsafe { *__errno_location() = 22 });
        return -(1 as i32);
    }
    v = value as uint32_t;
    return ibuf_set(
        buf,
        pos,
        &mut v as *mut uint32_t as *const (),
        ::core::mem::size_of::<uint32_t>() as u64,
    );
}
#[no_mangle]
pub extern "C" fn ibuf_set_h64(mut buf: *mut ibuf, mut pos: size_t, mut value: uint64_t) -> i32 {
    return ibuf_set(
        buf,
        pos,
        &mut value as *mut uint64_t as *const (),
        ::core::mem::size_of::<uint64_t>() as u64,
    );
}
#[no_mangle]
pub extern "C" fn ibuf_data(mut buf: *const ibuf) -> *mut () {
    return (unsafe { ((*buf).buf).offset((*buf).rpos as isize) }) as *mut ();
}
#[no_mangle]
pub extern "C" fn ibuf_size(mut buf: *const ibuf) -> size_t {
    return (unsafe { (*buf).wpos }).wrapping_sub(unsafe { (*buf).rpos });
}
#[no_mangle]
pub extern "C" fn ibuf_left(mut buf: *const ibuf) -> size_t {
    if (unsafe { (*buf).max }) == 0 as u64 {
        return 0 as size_t;
    }
    return (unsafe { (*buf).max }).wrapping_sub(unsafe { (*buf).wpos });
}
#[no_mangle]
pub extern "C" fn ibuf_truncate(mut buf: *mut ibuf, mut len: size_t) -> i32 {
    if ibuf_size(buf) >= len {
        (unsafe { (*buf).wpos = ((*buf).rpos).wrapping_add(len) });
        return 0;
    }
    if (unsafe { (*buf).max }) == 0 as u64 {
        (unsafe { *__errno_location() = 34 });
        return -(1 as i32);
    }
    return ibuf_add_zero(buf, len.wrapping_sub(ibuf_size(buf)));
}
#[no_mangle]
pub extern "C" fn ibuf_rewind(mut buf: *mut ibuf) {
    (unsafe { (*buf).rpos = 0 as size_t });
}
#[no_mangle]
pub extern "C" fn ibuf_close(mut msgbuf: *mut msgbuf, mut buf: *mut ibuf) {
    ibuf_enqueue(msgbuf, buf);
}
#[no_mangle]
pub extern "C" fn ibuf_from_buffer(
    mut buf: *mut ibuf,
    mut data: *mut (),
    mut len: size_t,
) {
    (unsafe {
        memset(
            buf as *mut (), 0,
            ::core::mem::size_of::<ibuf>() as u64,
        )
    });
    (unsafe { (*buf).buf = data as *mut u8 });
    (unsafe { (*buf).wpos = len });
    (unsafe { (*buf).size = (*buf).wpos });
    (unsafe { (*buf).fd = -(1 as i32) });
}
#[no_mangle]
pub extern "C" fn ibuf_from_ibuf(mut buf: *mut ibuf, mut from: *const ibuf) {
    ibuf_from_buffer(buf, ibuf_data(from), ibuf_size(from));
}
#[no_mangle]
pub extern "C" fn ibuf_get(
    mut buf: *mut ibuf,
    mut data: *mut (),
    mut len: size_t,
) -> i32 {
    if ibuf_size(buf) < len {
        (unsafe { *__errno_location() = 74 });
        return -(1 as i32);
    }
    (unsafe { memcpy(data, ibuf_data(buf), len) });
    (unsafe { (*buf).rpos = ((*buf).rpos as u64).wrapping_add(len) as size_t as size_t });
    return 0;
}
#[no_mangle]
pub extern "C" fn ibuf_get_ibuf(mut buf: *mut ibuf, mut len: size_t, mut new: *mut ibuf) -> i32 {
    if ibuf_size(buf) < len {
        (unsafe { *__errno_location() = 74 });
        return -(1 as i32);
    }
    ibuf_from_buffer(new, ibuf_data(buf), len);
    (unsafe { (*buf).rpos = ((*buf).rpos as u64).wrapping_add(len) as size_t as size_t });
    return 0;
}
#[no_mangle]
pub extern "C" fn ibuf_get_n8(mut buf: *mut ibuf, mut value: *mut uint8_t) -> i32 {
    return ibuf_get(
        buf,
        value as *mut (),
        ::core::mem::size_of::<uint8_t>() as u64,
    );
}
#[no_mangle]
pub extern "C" fn ibuf_get_n16(mut buf: *mut ibuf, mut value: *mut uint16_t) -> i32 {
    let mut rv: i32 = 0;
    rv = ibuf_get(
        buf,
        value as *mut (),
        ::core::mem::size_of::<uint16_t>() as u64,
    );
    (unsafe { *value = ntohs(*value) });
    return rv;
}
#[no_mangle]
pub extern "C" fn ibuf_get_n32(mut buf: *mut ibuf, mut value: *mut uint32_t) -> i32 {
    let mut rv: i32 = 0;
    rv = ibuf_get(
        buf,
        value as *mut (),
        ::core::mem::size_of::<uint32_t>() as u64,
    );
    (unsafe { *value = ntohl(*value) });
    return rv;
}
#[no_mangle]
pub extern "C" fn ibuf_get_n64(mut buf: *mut ibuf, mut value: *mut uint64_t) -> i32 {
    let mut rv: i32 = 0;
    rv = ibuf_get(
        buf,
        value as *mut (),
        ::core::mem::size_of::<uint64_t>() as u64,
    );
    (unsafe { *value = ntohll(*value) });
    return rv;
}
#[no_mangle]
pub extern "C" fn ibuf_get_h16(mut buf: *mut ibuf, mut value: *mut uint16_t) -> i32 {
    return ibuf_get(
        buf,
        value as *mut (),
        ::core::mem::size_of::<uint16_t>() as u64,
    );
}
#[no_mangle]
pub extern "C" fn ibuf_get_h32(mut buf: *mut ibuf, mut value: *mut uint32_t) -> i32 {
    return ibuf_get(
        buf,
        value as *mut (),
        ::core::mem::size_of::<uint32_t>() as u64,
    );
}
#[no_mangle]
pub extern "C" fn ibuf_get_h64(mut buf: *mut ibuf, mut value: *mut uint64_t) -> i32 {
    return ibuf_get(
        buf,
        value as *mut (),
        ::core::mem::size_of::<uint64_t>() as u64,
    );
}
#[no_mangle]
pub extern "C" fn ibuf_skip(mut buf: *mut ibuf, mut len: size_t) -> i32 {
    if ibuf_size(buf) < len {
        (unsafe { *__errno_location() = 74 });
        return -(1 as i32);
    }
    (unsafe { (*buf).rpos = ((*buf).rpos as u64).wrapping_add(len) as size_t as size_t });
    return 0;
}
#[no_mangle]
pub extern "C" fn ibuf_free(mut buf: *mut ibuf) {
    if buf.is_null() {
        return;
    }
    if (unsafe { (*buf).max }) == 0 as u64 {
        (unsafe { abort() });
    }
    if (unsafe { (*buf).fd }) != -(1 as i32) {
        (unsafe { close((*buf).fd) });
    }
    (unsafe { freezero((*buf).buf as *mut (), (*buf).size) });
    (unsafe { free(buf as *mut ()) });
}
#[no_mangle]
pub extern "C" fn ibuf_fd_avail(mut buf: *mut ibuf) -> i32 {
    return ((unsafe { (*buf).fd }) != -(1 as i32)) as i32;
}
#[no_mangle]
pub extern "C" fn ibuf_fd_get(mut buf: *mut ibuf) -> i32 {
    let mut fd: i32 = 0;
    fd = unsafe { (*buf).fd };
    (unsafe { (*buf).fd = -(1 as i32) });
    return fd;
}
#[no_mangle]
pub extern "C" fn ibuf_fd_set(mut buf: *mut ibuf, mut fd: i32) {
    if (unsafe { (*buf).max }) == 0 as u64 {
        (unsafe { abort() });
    }
    if (unsafe { (*buf).fd }) != -(1 as i32) {
        (unsafe { close((*buf).fd) });
    }
    (unsafe { (*buf).fd = fd });
}
#[no_mangle]
pub extern "C" fn ibuf_write(mut msgbuf: *mut msgbuf) -> i32 {
    let mut iov: [iovec; 1024] = [iovec {
        iov_base: std::ptr::null_mut::<()>(),
        iov_len: 0,
    }; 1024];
    let mut buf: *mut ibuf = std::ptr::null_mut::<ibuf>();
    let mut i: u32 = 0 as u32;
    let mut n: ssize_t = 0;
    (unsafe {
        memset(
            &mut iov as *mut [iovec; 1024] as *mut (), 0,
            ::core::mem::size_of::<[iovec; 1024]>() as u64,
        )
    });
    buf = unsafe { (*msgbuf).bufs.tqh_first };
    while !buf.is_null() {
        if i >= 1024 as u32 {
            break;
        }
        iov[i as usize].iov_base = ibuf_data(buf);
        iov[i as usize].iov_len = ibuf_size(buf);
        i = i.wrapping_add(1);
        buf = unsafe { (*buf).entry.tqe_next };
    }
    's_62: {
        loop {
            n = unsafe { writev((*msgbuf).fd, iov.as_mut_ptr(), i as i32) };
            if n == -(1 as i32) as i64 {
                if (unsafe { *__errno_location() }) == 4 {
                    continue;
                }
                if (unsafe { *__errno_location() }) == 105 {
                    (unsafe { *__errno_location() = 11 });
                }
                return -(1 as i32);
            } else {
                break 's_62;
            }
        }
    }
    if n == 0 as i64 {
        (unsafe { *__errno_location() = 0 });
        return 0;
    }
    msgbuf_drain(msgbuf, n as size_t);
    return 1;
}
#[no_mangle]
pub extern "C" fn msgbuf_init(mut msgbuf: *mut msgbuf) {
    (unsafe { (*msgbuf).queued = 0 as uint32_t });
    (unsafe { (*msgbuf).fd = -(1 as i32) });
    (unsafe { (*msgbuf).bufs.tqh_first = std::ptr::null_mut::<ibuf>() });
    (unsafe { (*msgbuf).bufs.tqh_last = &mut (*msgbuf).bufs.tqh_first });
}
extern "C" fn msgbuf_drain(mut msgbuf: *mut msgbuf, mut n: size_t) {
    let mut buf: *mut ibuf = std::ptr::null_mut::<ibuf>();
    let mut next: *mut ibuf = std::ptr::null_mut::<ibuf>();
    buf = unsafe { (*msgbuf).bufs.tqh_first };
    while !buf.is_null() && n > 0 as u64 {
        next = unsafe { (*buf).entry.tqe_next };
        if n >= ibuf_size(buf) {
            n = (n as u64).wrapping_sub(ibuf_size(buf)) as size_t as size_t;
            ibuf_dequeue(msgbuf, buf);
        } else {
            (unsafe { (*buf).rpos = ((*buf).rpos as u64).wrapping_add(n) as size_t as size_t });
            n = 0 as size_t;
        }
        buf = next;
    }
}
#[no_mangle]
pub extern "C" fn msgbuf_clear(mut msgbuf: *mut msgbuf) {
    let mut buf: *mut ibuf = std::ptr::null_mut::<ibuf>();
    loop {
        buf = unsafe { (*msgbuf).bufs.tqh_first };
        if buf.is_null() {
            break;
        }
        ibuf_dequeue(msgbuf, buf);
    }
}
#[no_mangle]
pub extern "C" fn msgbuf_write(mut msgbuf: *mut msgbuf) -> i32 {
    let mut iov: [iovec; 1024] = [iovec {
        iov_base: std::ptr::null_mut::<()>(),
        iov_len: 0,
    }; 1024];
    let mut buf: *mut ibuf = std::ptr::null_mut::<ibuf>();
    let mut buf0: *mut ibuf = std::ptr::null_mut::<ibuf>();
    let mut i: u32 = 0 as u32;
    let mut n: ssize_t = 0;
    let mut msg: msghdr = msghdr {
        msg_name: std::ptr::null_mut::<()>(),
        msg_namelen: 0,
        msg_iov: std::ptr::null_mut::<iovec>(),
        msg_iovlen: 0,
        msg_control: std::ptr::null_mut::<()>(),
        msg_controllen: 0,
        msg_flags: 0,
    };
    let mut cmsg: *mut cmsghdr = std::ptr::null_mut::<cmsghdr>();
    let mut cmsgbuf: C2RustUnnamed_2 = C2RustUnnamed_2 {
        hdr: cmsghdr {
            cmsg_len: 0,
            cmsg_level: 0,
            cmsg_type: 0,
            __cmsg_data: [],
        },
    };
    (unsafe {
        memset(
            &mut iov as *mut [iovec; 1024] as *mut (), 0,
            ::core::mem::size_of::<[iovec; 1024]>() as u64,
        )
    });
    (unsafe {
        memset(
            &mut msg as *mut msghdr as *mut (), 0,
            ::core::mem::size_of::<msghdr>() as u64,
        )
    });
    (unsafe {
        memset(
            &mut cmsgbuf as *mut C2RustUnnamed_2 as *mut (), 0,
            ::core::mem::size_of::<C2RustUnnamed_2>() as u64,
        )
    });
    buf = unsafe { (*msgbuf).bufs.tqh_first };
    while !buf.is_null() {
        if i >= 1024 as u32 {
            break;
        }
        if i > 0 as u32 && (unsafe { (*buf).fd }) != -(1 as i32) {
            break;
        }
        iov[i as usize].iov_base = ibuf_data(buf);
        iov[i as usize].iov_len = ibuf_size(buf);
        i = i.wrapping_add(1);
        if (unsafe { (*buf).fd }) != -(1 as i32) {
            buf0 = buf;
        }
        buf = unsafe { (*buf).entry.tqe_next };
    }
    msg.msg_iov = iov.as_mut_ptr();
    msg.msg_iovlen = i as size_t;
    if !buf0.is_null() {
        msg.msg_control =
            (unsafe { &mut cmsgbuf.buf }) as *mut [i8; 24] as caddr_t as *mut ();
        msg.msg_controllen = ::core::mem::size_of::<[i8; 24]>() as u64;
        cmsg = if msg.msg_controllen >= ::core::mem::size_of::<cmsghdr>() as u64 {
            msg.msg_control as *mut cmsghdr
        } else {
            std::ptr::null_mut::<cmsghdr>()
        };
        (unsafe {
            (*cmsg).cmsg_len = ((::core::mem::size_of::<cmsghdr>() as u64)
                .wrapping_add(::core::mem::size_of::<size_t>() as u64)
                .wrapping_sub(1 as i32 as u64)
                & !(::core::mem::size_of::<size_t>() as u64).wrapping_sub(1 as i32 as u64))
            .wrapping_add(::core::mem::size_of::<i32>() as u64)
        });
        (unsafe { (*cmsg).cmsg_level = 1 });
        (unsafe { (*cmsg).cmsg_type = SCM_RIGHTS as i32 });
        (unsafe { *(((*cmsg).__cmsg_data).as_mut_ptr() as *mut i32) = (*buf0).fd });
    }
    's_123: {
        loop {
            n = unsafe { sendmsg((*msgbuf).fd, &mut msg, 0) };
            if n == -(1 as i32) as i64 {
                if (unsafe { *__errno_location() }) == 4 {
                    continue;
                }
                if (unsafe { *__errno_location() }) == 105 {
                    (unsafe { *__errno_location() = 11 });
                }
                return -(1 as i32);
            } else {
                break 's_123;
            }
        }
    }
    if n == 0 as i64 {
        (unsafe { *__errno_location() = 0 });
        return 0;
    }
    if !buf0.is_null() {
        (unsafe { close((*buf0).fd) });
        (unsafe { (*buf0).fd = -(1 as i32) });
    }
    msgbuf_drain(msgbuf, n as size_t);
    return 1;
}
#[no_mangle]
pub extern "C" fn msgbuf_queuelen(mut msgbuf: *mut msgbuf) -> uint32_t {
    return unsafe { (*msgbuf).queued };
}
extern "C" fn ibuf_enqueue(mut msgbuf: *mut msgbuf, mut buf: *mut ibuf) {
    if (unsafe { (*buf).max }) == 0 as u64 {
        (unsafe { abort() });
    }
    (unsafe { (*buf).entry.tqe_next = std::ptr::null_mut::<ibuf>() });
    (unsafe { (*buf).entry.tqe_prev = (*msgbuf).bufs.tqh_last });
    (unsafe { *(*msgbuf).bufs.tqh_last = buf });
    (unsafe { (*msgbuf).bufs.tqh_last = &mut (*buf).entry.tqe_next });
    (unsafe { (*msgbuf).queued = ((*msgbuf).queued).wrapping_add(1) });
}
extern "C" fn ibuf_dequeue(mut msgbuf: *mut msgbuf, mut buf: *mut ibuf) {
    if !(unsafe { (*buf).entry.tqe_next }).is_null() {
        (unsafe { (*(*buf).entry.tqe_next).entry.tqe_prev = (*buf).entry.tqe_prev });
    } else {
        (unsafe { (*msgbuf).bufs.tqh_last = (*buf).entry.tqe_prev });
    }
    (unsafe { *(*buf).entry.tqe_prev = (*buf).entry.tqe_next });
    (unsafe { (*msgbuf).queued = ((*msgbuf).queued).wrapping_sub(1) });
    ibuf_free(buf);
}
